package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Ad;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Recommend;
import com.rzico.basics.model.*;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.ProductService;
import com.rzico.basics.service.RecommendTagService;
import com.rzico.basics.service.ShopService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;


/**
 * 推荐 Tag
 *
 */
@Api(description = "推荐标签接口")
@RestController
@RequestMapping("/recommendTag")
public class RecommendTagController {

    @Autowired
    RecommendTagService recommendTagService;

    @Autowired
    ProductService productService;

    @Autowired
    MemberService memberService;

    @Autowired
    ShopService shopService;


    /**
     * 查询标签下的推荐店铺
     *
     * @return
     */
    @ApiOperation("分页查询设计器")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId",  value = "商户号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签",   dataType = "String", paramType = "query")
    })
    public CommResult<Recommend> list(Double lat, Double lng, String mchId, Long[] tagIds, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();

        if (lat==null) {
            lat = 0d;
        }
        if (lng==null) {
            lng = 0d;
        }

        if (tagIds!=null && tagIds.length>0) {
            params.put("tagIds",tagIds);
        }

        if (tagIds == null || tagIds.length == 0) {
            return null;
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Recommend> list = recommendTagService.getRecommendByTags(params);
        PageResult<Recommend> pageResult = new PageResult<Recommend>(list, startPage.getTotal(), pageable);
        for (Recommend vo:list) {
            if (vo.getType().equals(0)) {
                Map<String,Object> voParams = new HashMap<>();
                voParams.put("id",vo.getTypeId());

                List<ProductVo> products = productService.selectProductVoList(voParams);
                if (products.size()>0) {
                    ProductVo productVo = products.get(0);
                    vo.setProductVo(productVo);

                    Member member = memberService.getCurrent();

                    if (member!=null && member.getVvip().compareTo(1)>0) {
                        productVo.setVipPrice(productVo.getMemberPrice(member));
                    } else {
                        productVo.setVipPrice(productVo.getVip1price());
                        if (productVo.getVipPrice().compareTo(BigDecimal.ZERO)==0) {
                            productVo.setVipPrice(productVo.getPrice());
                        }
                    }

                    if (member!=null && member.getAmount().compareTo(BigDecimal.ZERO)>0) {
                        List<PromotionVo> pvo = new ArrayList<>();
                        for (PromotionVo pvvo:productVo.getPromotions()) {
                            if (pvvo.getType().equals(10)) {
                                continue;
                            }
                            if (pvvo.getFirsted()) {
                                continue;
                            }
                            if (productVo.getFreightId()==null && vo.getType().equals(6)) {
                                continue;
                            }
                            pvo.add(pvvo);
                        }
                        productVo.setPromotions(pvo);
                    }
                }

            } else
            {
                Map<String, Object> voParams = new HashMap<String, Object>();

                voParams.put("id",vo.getTypeId());
                voParams.put("status",1);
                //List<ShopVo> shopVo1 = shopService.selectLbsList(voParams,lat,lng);
                ShopVo shopVo1 = shopService.findShopVoParams(voParams);
                if (shopVo1 != null) {
                    if (lat!=null && lng !=null) {
                            if (shopVo1.getLat() != null && shopVo1.getLng() != null) {
                                MapPoint point = new MapPoint();
                                point.setLng(lng);
                                point.setLat(lat);

                                MapPoint shopPoint = new MapPoint();
                                shopPoint.setLat(shopVo1.getLat());
                                shopPoint.setLng(shopVo1.getLng());

                                Double d = point.distatce(shopPoint);
                                shopVo1.setDistance(d);
                            }
                        }

                     vo.setShopVo(shopVo1);
                     vo.setDistance(vo.getShopVo().getDistance());
                 }

//                if (shopVo.size()>0) {
//                    vo.setShopVo(shopVo.get(0));
//                    vo.setDistance(vo.getShopVo().getDistance());
//                }

            }
        }

        //list.removeIf(vo -> vo.getShopVo() == null);

//        List<Recommend> data = pageResult.getData();
//        //开始排序
        Collections.sort(list, new Comparator<Recommend>() {
            public int compare(Recommend n1, Recommend n2) {
                if (n1.getDistance()==null) {
                    return -1;
                }
                if (n2.getDistance()==null) {
                    return 1;
                }
                return n1.getDistance().compareTo(n2.getDistance());
            }
        });

        return CommResult.success(pageResult);

    }



}
